package com.lyncs.ods.modules.txn.controller;


import com.lyncs.ods.common.api.CommonPage;
import com.lyncs.ods.common.api.CommonResult;
import com.lyncs.ods.modules.txn.service.OrderService;
import com.lyncs.ods.req.ListPageSearchReq;
import com.lyncs.ods.req.SaveOrderReq;
import com.lyncs.ods.resp.*;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 预约单总表 前端控制器
 * </p>
 *
 * @author alex
 * @since 2022-02-05
 */
@RestController
@RequestMapping("/txn/order")
@ApiModel("交易相关API")
@Api(tags = {"订单相关接口"})
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping("save")
    @ApiOperation(value = "创建/修改 订单")
    public CommonResult<Long> saveOrder(@Valid @RequestBody SaveOrderReq req) {
        return CommonResult.success(orderService.saveOrder(req));
    }

    @PostMapping("list")
    @ApiOperation(value = "获取 订单列表")
    public CommonResult<CommonPage<ListPageResp>> listOrder(@RequestBody ListPageSearchReq req) {
        return CommonResult.success(orderService.listOrder(req));
    }

    @GetMapping("{orderId}/detail")
    @ApiOperation(value = "获取 订单详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单ID", dataType = "Long", paramType = "path"),
    })
    public CommonResult<OrderDetailInfoResp> getOrderDetail(@PathVariable Long orderId) {
        return CommonResult.success(orderService.getOrderDetail(orderId));
    }

    @GetMapping("{orderId}/top")
    @ApiOperation(value = "置顶/取消置顶")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单ID", dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "type", value = "操作类型:0.取消置顶;1.置顶", dataType = "Integer", paramType = "query"),
    })
    public CommonResult<?> stickyOnTop(@PathVariable Long orderId, @RequestParam Integer type) {
        orderService.stickyOnTop(orderId, type);
        return CommonResult.success();
    }

    @GetMapping("{orderId}/reverse")
    @ApiOperation(value = "撤销订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单ID", dataType = "Long", paramType = "path"),
    })
    public CommonResult<?> reverseOrder(@PathVariable Long orderId) {
        orderService.reverseOrder(orderId);
        return CommonResult.success();
    }

    @GetMapping("{orderId}/editLog")
    @ApiOperation(value = "订单变更历史")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单ID", dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "page", value = "当前页数，默认1", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "page_size", value = "当前页请求数量，默认20", dataType = "Integer", paramType = "query"),
    })
    public CommonResult<CommonPage<TxnEditLogResp>> getOrderEditLog(@PathVariable Long orderId,
                                                                    @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                                                    @RequestParam(value = "page_size", required = false, defaultValue = "20") Integer pageSize) {
        return CommonResult.success(orderService.getOrderEditLog(orderId, page, pageSize));
    }

    @GetMapping("{orderId}/log")
    @ApiOperation(value = "订单交付历史")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单ID", dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "sku_id", value = "SKU ID，非必填，传此值表示获取单个sku交付历史和sku详情", dataType = "Long", paramType = "query"),
    })
    public CommonResult<OrderDeliverLogResp> getOrderSkuDeliverLog(@PathVariable Long orderId, @RequestParam(value = "sku_id", required = false) Long skuId) {
        return CommonResult.success(orderService.getOrderSkuDeliverLog(orderId, skuId));
    }
}

